----------------------------------------------------------------------------------
-- Company: 
-- Engineer: 
-- 
-- Create Date:    10:46:02 04/26/2012 
-- Design Name: 
-- Module Name:    brick - Behavioral 
-- Project Name: 
-- Target Devices: 
-- Tool versions: 
-- Description: 
--
-- Dependencies: 
--
-- Revision: 
-- Revision 0.01 - File Created
-- Additional Comments: 
--
----------------------------------------------------------------------------------
library IEEE, work;
use IEEE.STD_LOGIC_1164.ALL;
use work.game_support.all;

-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
use IEEE.NUMERIC_STD.ALL;

-- Uncomment the following library declaration if instantiating
-- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity brick_controller is
	Generic (brick_position : in position);
	Port (
		clk : in std_logic;
		ball_position : in position;
		h_count : in std_logic_vector(10 downto 0);
		v_count : in std_logic_vector(10 downto 0);
		hit : out std_logic;
		here : out std_logic
	);
end brick_controller;

architecture Behavioral of brick_controller is

	signal ball_on_brick, last_ball_on_brick : std_logic;

begin

	process(clk)
	begin
		if rising_edge(clk) then
			last_ball_on_brick <= ball_on_brick;
		end if;
	end process;
	
	hit <= ball_on_brick and not last_ball_on_brick;

	ball_on_brick <= '1' when
		ball_position.x >= brick_position.x and 
		ball_position.x < brick_position.x + BRICK_BOUNDS.width and
		ball_position.y >= brick_position.y and
		ball_position.y < brick_position.y + BRICK_BOUNDS.height
	else '0';

	

	here <= '1' when
		unsigned(h_count) >= brick_position.x and
		unsigned(h_count) < (brick_position.x + BRICK_BOUNDS.width) and
		unsigned(v_count) >= brick_position.y and
		unsigned(v_count) < (brick_position.y + BRICK_BOUNDS.height) and
		ball_on_brick = '0'
	else '0';


end Behavioral;

